#!/bin/bash

# try to directly access the HAT via /sys
if [ -f /proc/device-tree/hat/product ]; then
  PRODUCT=$(tr -d '\0' </proc/device-tree/hat/product)
  VENDOR=$(tr -d '\0' </proc/device-tree/hat/vendor)
else
  EEPFILE=/tmp/eeprom.eep
  TXTFILE=/tmp/eeprom.txt

  modprobe i2c_dev
  modprobe at24

  # Bus number is different on Pi3 and 4
  for i in 7 3 11 22; do
    if [ -d /sys/class/i2c-adapter/i2c-$i ]; then
      DEVID=$i
      break
    fi
  done

  if [ ! -d "/sys/class/i2c-adapter/i2c-$DEVID/$DEVID-0050" ]; then
    echo "24c32 0x50" > /sys/class/i2c-adapter/i2c-$DEVID/new_device
  fi

  dd if=/sys/class/i2c-adapter/i2c-$DEVID/$DEVID-0050/eeprom of=$EEPFILE 2>/dev/null
  
  /usr/bin/eepdump $EEPFILE $TXTFILE >/dev/null
  if [ -f $TXTFILE ]; then 
    VENDOR=`cat $TXTFILE | grep "vendor " | awk -F\" '{print $2}' | xargs echo`
    PRODUCT=`cat $TXTFILE | grep "product " | awk -F\" '{print $2}' | xargs echo`

    if [ "$VENDOR" == "" ]; then
      VENDOR="no vendor"
    fi
    if [ "$PRODUCT" == "" ]; then
      PRODUCT="no product"
    fi
  fi
fi

echo $VENDOR:$PRODUCT
rm $TXTFILE 2>/dev/null
